package com.cardbaobao.cardbabyclient.utils;

import android.annotation.SuppressLint;

import org.joda.time.DateTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author zad
 */
@SuppressLint("SimpleDateFormat")
public class DateTimeUtil {

    /**
     * 日期统一格式
     */
    private final static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 日期统一格式
     */
    private final static SimpleDateFormat format_datatime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取 yyyy-MM-dd 格式时间
     *
     * @param dateTime
     * @return String
     */
    public static String getFormatDate(String dateTime) {
        try {
            return format.format(format.parse(dateTime));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    /**
     * 获取 yyyy-MM-dd HH:mm:ss 格式时间
     *
     * @return String
     */
    public static String getFormatDateString() {
        long lt = new Long(String.valueOf(System.currentTimeMillis()));
        Date date = new Date(lt);
        String res = format_datatime.format(date);
        return res;
    }

    public static String getDateTime() {
        return format.format(new Date(System.currentTimeMillis()));
    }

    /**
     * 获取当前年份
     *
     * @return
     */
    public static String getYear() {
        Calendar calendar = Calendar.getInstance();//首先要获取日历对象
        return String.valueOf(calendar.get(Calendar.YEAR)); // 获取当前年份
    }

    /**
     * 得到现在时间
     *
     * @return
     */
    public static Date getNow() {
        Date currentTime = new Date();
        return currentTime;
    }

    /**
     * 字符串时间转换为Date
     *
     * @param dateTime 字符串时间
     * @return
     */
    public static Date getDate(String dateTime) {
        Date date = getNow();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
        try {
            date = sdf.parse(dateTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 比较时间是哪天
     *
     * @param date
     * @return
     */
    public static String getDateDayTime(Date date) {
        String result = "";
        DateTime today = new DateTime(getNow());
        DateTime otherDay = new DateTime(date);
        if (otherDay.toString("yy-MM-dd").equals(today.toString("yy-MM-dd"))) {
            result = "今天";
        } else if (otherDay.plusDays(1).toString("yy-MM-dd").equals(today.toString("yy-MM-dd"))) {
            result = "昨天 ";
        } else if (otherDay.plusDays(2).toString("yy-MM-dd").equals(today.toString("yy-MM-dd"))) {
            result = "前天 ";
        }
        if (result.isEmpty()) {
            return getMonthAndDay(date);
        } else {
            return result + "        " + getHourAndMin(date);
        }

    }

    public static String getDateDayTimeNew(Date date) {
        String result = "";
        DateTime today = new DateTime(getNow());
        DateTime otherDay = new DateTime(date);
        if (otherDay.toString("yy-MM-dd").equals(today.toString("yy-MM-dd"))) {
            result = "今天";
        } else if (otherDay.plusDays(1).toString("yy-MM-dd").equals(today.toString("yy-MM-dd"))) {
            result = "昨天 ";
        } else if (otherDay.plusDays(2).toString("yy-MM-dd").equals(today.toString("yy-MM-dd"))) {
            result = "前天 ";
        }
        if (result.isEmpty()) {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
        } else {
            return result + "    " + getHourAndMin(date);
        }

    }

    /**
     * 返回月份跟 天
     *
     * @param date
     * @return
     */
    public static String getMonthAndDay(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
        return format.format(date);
    }

    /**
     * 返回时跟分
     *
     * @param date
     * @return
     */
    public static String getHourAndMin(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(date);
    }


}
